package com.ruoyi.station.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.station.domain.SMachineCabinet;
import com.ruoyi.station.domain.SStation;
import com.ruoyi.station.mapper.SStationMapper;
import com.ruoyi.station.service.SStationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 站点Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-05
 */
@Slf4j
@Service
public class SStationServiceImpl extends ServiceImpl<SStationMapper, SStation> implements SStationService {

    @Autowired
    private SStationMapper sStationMapper;
    /**
     * 导入站点数据
     * @param file
     * @return
     */
    @Override
    @Transactional
    public String importsSStation(MultipartFile file) {
        String username = SecurityUtils.getUsername();
        List<List<Object>> lists = FileUtils.parseUploadFile(file);
        int i = 3;
        for (List<Object> readStation : lists) {
            //站点名称
            String stationName = String.valueOf(readStation.get(1)).trim();
            SStation sStation = sStationMapper.selectOne(new LambdaQueryWrapper<SStation>().eq(SStation::getStationName, stationName));
            if (sStation != null) return "第" + i +"行站点名称已存在！";
            //站点类型
            String stationType = DictUtils.getDictValue("station_type", String.valueOf(readStation.get(2)).trim());
            if(StringUtils.isBlank(stationType)) return "第" + i +"行站点类型不存在！";
            //电压等级
            String KV = DictUtils.getDictValue("KV", String.valueOf(readStation.get(3)).trim());
            if(StringUtils.isBlank(KV)) return "第" + i +"行电压等级不存在！";
            //所属区域
            String area = DictUtils.getDictValue("area", String.valueOf(readStation.get(4)).trim());
            if(StringUtils.isBlank(area)) return "第" + i +"行所属区域不存在！";
            //投运日期
            String operationDate = String.valueOf(readStation.get(5)).trim();
            if(StringUtils.isBlank(operationDate)) return "第" + i +"行投运日期不能为空！";
            //联系电话
            String tel = String.valueOf(readStation.get(6)).trim();
            if(StringUtils.isBlank(tel)) return "第" + i +"行联系电话不正确！";
            //光缆出口数量
            String opticalOutNum = String.valueOf(readStation.get(7)).trim();
            if(StringUtils.isBlank(opticalOutNum)) return "第" + i +"行光缆出口数量不正确！";
            //经度
            String longitude = String.valueOf(readStation.get(8)).trim();
            if(StringUtils.isBlank(longitude)) return "第" + i +"行经度不正确！";
            //纬度 i want my tears back
            String latitude = String.valueOf(readStation.get(9)).trim();
            if(StringUtils.isBlank(latitude)) return "第" + i +"行纬度不正确！";
            //产权单位
            String propertyUnit = String.valueOf(readStation.get(10)).trim();
            if(StringUtils.isBlank(propertyUnit)) return "第" + i +"行产权单位不正确！";
            //封装对象
            SStation build = SStation.builder()
                    .stationName(stationName)
                    .stationType(stationType)
                    .kv(KV)
                    .area(area)
                    .operationDate(DateUtil.parseYYYYMMDDDate(operationDate))
                    .tel(tel)
                    .opticalOutNum(Long.parseLong(opticalOutNum))
                    .longitude(longitude)
                    .latitude(latitude)
                    .propertyUnit(propertyUnit)
                    .createBy(username)
                    .createTime(cn.hutool.core.date.DateUtil.date())
                    .build();
            int insert = sStationMapper.insert(build);
            if (insert < 1) return "第" + i +"行保存出错了！";
            i++;
        }
        return "成功导入"+lists.size()+"条";
    }
}
